package top.hzy520.dept.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import top.hzy520.common.constants.Constants;
import top.hzy520.common.resp.ApiResult;
import top.hzy520.dept.entity.Dept;
import top.hzy520.dept.service.DeptService;
import top.hzy520.user.entity.User;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 15:33 2018/7/31
 */
@Controller
public class DeptController {
    @Autowired
    @Qualifier("deptServiceImpl")
    private DeptService deptService;

    @ResponseBody
    @RequestMapping("/dept/list")
    public List<Dept> deptList(){
        List<Dept>  deptList=deptService.selectDeptList();
        return deptList;
    }
    @RequestMapping("/dept/add/{id}")
    public String deptAdd(@PathVariable("id") Long id){
       return "admin/dept/add";
    }
    @RequestMapping("/dept/edit/{deptId}")
    public String deptEdit(@PathVariable("deptId") Long deptId, Model model){
      Dept dept= deptService.selectDeptById(deptId);
      dept.setParentDept(deptService.selectDeptById(dept.getPId()));
       model.addAttribute("dept",dept);
        return "admin/dept/edit";
    }

     @PostMapping("/checkDeptNameUnique")
     @ResponseBody
    public boolean checkDeptNameUnique(Dept dept){
      boolean  uniqueFlag= deptService.checkDeptNameUnique(dept);
         return uniqueFlag;
    }

    @PostMapping("/dept/editpost")
    @ResponseBody
    public ApiResult editDeptPost(Dept dept){
        dept.setPId(dept.getParentId());
        deptService.editDeptPost(dept);
    ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"编辑成功");
    return apiResult;
    }
    @PostMapping("/dept/addpost")
    @ResponseBody
    public ApiResult addDeptPost(Dept dept){
        dept.setPId(dept.getParentId());
        deptService.addDeptPost(dept);
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"添加成功");
        return apiResult;
    }
    @PostMapping("/dept/addpost/{deptId}")
    @ResponseBody
    public ApiResult removeDeptPost(@PathVariable("deptId") Long deptId){
        deptService.removeDeptPost(deptId);
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"添加成功");
        return apiResult;
    }
}
